package com.ruitu.health_1.ui.fragment.order;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.google.gson.Gson;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.support.widget.progress.ProgressLayout;
import com.ruitu.health_1.R;
import com.ruitu.health_1.dao.bean.GoodsInfo;
import com.ruitu.health_1.dao.bean.OrderItemList;
import com.ruitu.health_1.dao.bean.OrderList;
import com.ruitu.health_1.dao.bean.StoreOrderItemList;
import com.ruitu.health_1.mvp.model.OrderModel;
import com.ruitu.health_1.mvp.presenter.OrderPresenter;
import com.ruitu.health_1.ui.CommentActivity;
import com.ruitu.health_1.ui.CommentListActivity;
import com.ruitu.health_1.ui.OrderDetailsActivity;
import com.ruitu.health_1.view.NoCacheLoadingFragment;
import com.ruitu.router_module.AppHolder;

import java.util.ArrayList;
import java.util.List;

public class CompletedFragment extends NoCacheLoadingFragment<OrderPresenter.OrderP, OrderModel.OrderM> {
    public static CompletedFragment newInstance() {

        Bundle args = new Bundle();

        CompletedFragment fragment = new CompletedFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_order_list;
    }

    private List<Integer> skipId;

    @Override
    protected void initViews(View v) {
        skipId = new ArrayList<>();
        showLoadingView(skipId);
        getDataList();
    }

    private void getDataList() {
        p.orderList(1, size, AppHolder.instance().getUser().getId(), "3");
    }

    private StoreDataAdapter adapter;

    @Override
    protected ProgressLayout findProgressLayout(View v) {
        return null;
    }

    @Override
    protected LRecyclerView findRecyclerView(View v) {
        return v.findViewById(R.id.l_recyclerview);
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new StoreDataAdapter(getContext());
        return adapter;
    }

    @Override
    protected void reqFirst() {
        size = 10;
        getDataList();
    }

    private int size = 10;

    @Override
    protected void reqMore() {
        size = size + 10;
        getDataList();
    }

    @Override
    public void setData(List<StoreOrderItemList> orderLists) {
        showContent(skipId);
        adapter.setDataList(orderLists);
    }

    //获取所有确认收货订单的id
    private List<String> ids(StoreOrderItemList storeOrderItemList) {
        idList = new ArrayList<>();
        List<OrderItemList> orderItemList = storeOrderItemList.getOrderItemList();
        for (int i = 0; i < orderItemList.size(); i++) {
            idList.add(orderItemList.get(i).getId());
        }
        return idList;
    }

    class DataAdapter extends ListBaseAdapter<OrderList> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_order_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {
            final OrderList orderList = mDataList.get(position);
            RecyclerView recyclerview = holder.getView(R.id.recyclerview);
            View view = holder.getView(R.id.divider);

            view.setVisibility(position > 0 ? View.VISIBLE : View.GONE);
            List<OrderItemList> orderItemList = orderList.getStoreOrderItemList().get(position).getOrderItemList();
            LinearLayoutManager manager = new LinearLayoutManager(mContext);

            recyclerview.setLayoutManager(manager);
            ArrayList<StoreOrderItemList> storeOrderItemList = orderList.getStoreOrderItemList();
            for (int i = 0; i < storeOrderItemList.size(); i++) {
                storeOrderItemList.get(i).setOrderStatus(orderList.getOrder().getStatus());
            }
            StoreDataAdapter childDataAdapter = new StoreDataAdapter(mContext);
            recyclerview.setAdapter(childDataAdapter);
//            childDataAdapter.setDataList(storeOrderItemList);

        }
    }

    private ArrayList idList;

    class StoreDataAdapter extends ListBaseAdapter<StoreOrderItemList> {
        public StoreDataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_store_order;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            LinearLayout ll_shipped = holder.getView(R.id.ll_shipped);
            RecyclerView recyclerview = holder.getView(R.id.l_recyclerview);
            TextView tv_store_name = holder.getView(R.id.tv_store_name);
            LinearLayout ll_complete = holder.getView(R.id.ll_complete);
            TextView tv_comment = holder.getView(R.id.tv_comment);
            TextView tv_buy_again = holder.getView(R.id.tv_buy_again);
            TextView tv_complete_detail = holder.getView(R.id.tv_complete_detail);
            ll_complete.setVisibility(View.VISIBLE);
            final StoreOrderItemList orderItemList = mDataList.get(position);
            tv_store_name.setText(orderItemList.getName());
            recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
            ChildDataAdapter childDataAdapter = new ChildDataAdapter(mContext);
            recyclerview.setAdapter(childDataAdapter);
            childDataAdapter.setDataList(orderItemList.getOrderItemList());
            tv_comment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (orderItemList.getOrderItemList().size() <= 1) {
                        Bundle bundle = new Bundle();
                        OrderItemList orderItemList1 = orderItemList.getOrderItemList().get(0);
                        bundle.putString("orderId", orderItemList1.getId());
                        bundle.putString("pic", orderItemList1.getProductPic());
                        startActivity(CommentActivity.class, bundle);
                        return;
                    }
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("order", orderItemList.getOrderItemList());
                    startActivity(CommentListActivity.class, bundle);
                }
            });

            tv_buy_again.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            tv_complete_detail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Bundle bundle = new Bundle();
                    bundle.putSerializable("orderId", new Gson().toJson(ids(orderItemList)));
                    startActivity(OrderDetailsActivity.class, bundle);
                }
            });
        }
    }

    class ChildDataAdapter extends ListBaseAdapter<OrderItemList> {

        public ChildDataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_child_order;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            ImageView iv_commodity = holder.getView(R.id.iv_commodity);
            TextView tv_commodity_name = holder.getView(R.id.tv_commodity_name);
            TextView tv_commodity_info = holder.getView(R.id.tv_commodity_info);
            TextView tv_commodity_price = holder.getView(R.id.tv_commodity_price);
            TextView tv_commodity_num = holder.getView(R.id.tv_commodity_num);

            OrderItemList listBean = mDataList.get(position);
            Glide.with(mContext).load(listBean.getProductPic()).apply(Arad.opts).into(iv_commodity);
            StringBuffer buffer = new StringBuffer();
            GoodsInfo good = listBean.getGood();
            if (good != null) {
                if (!TextUtils.isEmpty(good.getSp1())) {
                    buffer.append(good.getSp1());
                }
                if (!TextUtils.isEmpty(good.getSp2())) {
                    buffer.append("," + good.getSp2());
                }
                if (!TextUtils.isEmpty(good.getSp3())) {
                    buffer.append("," + good.getSp3());
                }
            }
            tv_commodity_info.setText(buffer.toString());
            tv_commodity_name.setText(listBean.getProductName());
//            tv_commodity_num.setText(listBean.getRealAmount());
            tv_commodity_price.setText(listBean.getRealAmount());

        }
    }
}
